package com.gict.antifraudback.controller.web.spider;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gict.antifraudback.entity.Article;
import com.gict.antifraudback.entity.Spider;
import com.gict.antifraudback.exception.BusinessException;
import com.gict.antifraudback.exception.MyException;
import com.gict.antifraudback.result.ResultEnum;
import com.gict.antifraudback.result.ResultView;
import com.gict.antifraudback.service.SpiderService;
import com.gict.antifraudback.vo.SpiderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author ThinkPad
 */
@RestController
@Slf4j
@RequestMapping("/wapi/spider")
public class SpiderController {

    @Autowired
    private SpiderService spiderService;

    @GetMapping("/getList")
    public ResultView getList(SpiderVo spiderVo) {
        LambdaQueryWrapper<Spider> wrapper = Wrappers.lambdaQuery();
        wrapper.like(!ObjectUtils.isEmpty(spiderVo.getName()), Spider::getName, spiderVo.getName());
        return ResultView.ok(spiderService.page(new Page<>(spiderVo.getCurrent(), spiderVo.getPageSize()), wrapper));
    }

    @GetMapping("/getAllSpider")
    public ResultView getAllSpider() {
        return ResultView.ok(spiderService.list());

    }

    @GetMapping("/get")
    public ResultView get(@Valid @NotNull Integer id) {
        Spider spider = spiderService.getById(id);
        if (spider == null) {
            throw new MyException("规则不存在");
        }
        return ResultView.ok(spider);
    }

    @PostMapping("/spiderArticle")
    public ResultView spiderArticle(@RequestBody Map<String, Object> map) {
        Object id = map.get("id");
        Object url = map.get("url");
        if (id == null) {
            throw new BusinessException("id is null");
        }
        if (ObjectUtils.isEmpty(url)) {
            throw new BusinessException("url is null");
        }
        Spider spider = spiderService.getById(Integer.valueOf(id.toString()));
        if (spider == null) {
            throw new MyException("规则不存在");
        }
        Article article;
        try {
            article = spiderService.spiderArticle(spider, url.toString());
        } catch (IOException e) {
            throw new BusinessException("抓取失败！");
        }
        return ResultView.ok(article);
    }

    @PostMapping("/delete")
    public ResultView delete(@RequestBody ArrayList<Integer> ids) {
        log.info("spiderVo:{}", ids);
        for (Integer id : ids) {
            spiderService.removeById(id);
        }
        return ResultView.okMsg("删除成功");
    }

    @PostMapping("/add")
    public ResultView add(@Valid @RequestBody Spider spider) {
        this.createBefore(spider);
        spiderService.save(spider);
        return ResultView.ok();
    }

    /**
     * 创建前检查
     */
    private void createBefore(Spider spider) {
        LambdaQueryWrapper<Spider> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Spider::getName, spider.getName());
        Spider exists = spiderService.getOne(wrapper);

        if (exists != null && !exists.getId().equals(spider.getId())) {
            throw new MyException(ResultEnum.CODE_2, "该规则已经存在");
        }
    }

    @PostMapping("/update")
    public ResultView update(@RequestBody @Valid Spider spider) {
        Spider update = spiderService.getById(spider.getId());
        if (update == null) {
            return ResultView.error(ResultEnum.CODE_2, "数据不存在！");
        }
        createBefore(spider);
        spiderService.updateById(spider);
        return ResultView.ok();
    }
}
